package com.xxx.store.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.xxx.store.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.sql.DataSource;
import java.sql.Connection;
import java.util.List;

/**
 * TODO
 *
 * @ClassName
 * @Author FGH
 * @Date 2023/4/3 6:32 下午
 **/
public class BaseController<E, S extends IService>{

    @Autowired
    S service;



    @GetMapping("/getById")
    public Result getById(Integer id){
        return Result.ok(service.getById(id));
    }

    @GetMapping("/list")
    public Result list(){
        List<E> list = service.list(
                new QueryWrapper<E>().orderByDesc("create_time")
        );
        return Result.ok(list);
    }

    @PostMapping("/add")
    public Result add(@RequestBody E t){
        service.save(t);
        return Result.ok(t);
    }

    @PostMapping("/update")
    public Result update(@RequestBody E t){
        service.updateById(t);
        return Result.ok("更新成功");
    }

    // 删除一个
    @GetMapping("/del")
    public Result del(Integer id){
        service.removeById(id);
        return Result.ok("删除成功");
    }
}
